/**
 * Created by Kpeng on 2017/7/18.
 * Depending on the jquery-1.11.3.min.js.
 * Depending on the style.css.
 * To cite this document, always state the source as shown above.
 */
$(function () {
    wzERP.Post.init();
});


wzERP.Post.init = function () {
    // wzERP.Common.verifier('#countriesList');
    wzERP.Post.addHandle();//添加岗位
    wzERP.Post.editHandle();//编辑岗位
    wzERP.Post.delHandle(); //删除岗位
};

//添加岗位
wzERP.Post.addHandle = function () {
    var opt = {
        ele: '.addBtn',
        modalBox: '#modal',
        modalTitleStr: '添加岗位',
        onApprove: function () {
            if(!!wzERP.Post.checkform('#searchForm')){
                layer.msg('填写项不符合规则', {icon: 2});
                return false;
            }
            wzERP.Common.saveHandle('#position', '#submitPositionUrl', $(this).find('.setId'),{},function(){
                wzERP.Common.delayReload(wzERP.delayTime);
            });

        },
        snippets: $("#addUpdatePositionHtml").val()
    };
    wzERP.Common.handleData(opt);
};

//表单验证出口
wzERP.Post.checkform = function (ele) {
    /*Arg Guide
     @ ele 要验证的表单
     */
    $(ele).form('submit');
    return $(ele).hasClass('error');
}

//编辑岗位
wzERP.Post.editHandle = function () {
    var opt = {
        ele: '.editBtn',
        modalBox: '#modal',
        modalTitleStr: '编辑岗位',
        onApprove: function () {
            if(!!wzERP.Post.checkform('#searchForm')){
                layer.msg('填写项不符合规则', {icon: 2});
                return false;
            }
            wzERP.Common.saveHandle('#position', '#updatePositionUrl', $(this).find('.setId'),wzERP.Common.delayReload(wzERP.delayTime));
        },
        snippets: $("#addUpdatePositionHtml").val()
    };
    wzERP.Common.handleData(opt);
};


//删除岗位
wzERP.Post.delHandle = function () {
    /*var opt = {
        ele: '.delBtn',
        modalBox: '#modal',
        modalTitleStr: '',
        onApprove: function () {
            wzERP.Common.deleteData('#delPositionUrl', $(this).find('.setId'));
        },
        snippets: $("#positionDeleteHtml").val()
    };
    wzERP.Common.handleData(opt);*/
    
    $('#main').on('click', '.delBtn', function () {
        var $self = $(this);
        layer.alert('确定删除该岗位吗？', {
                title: '提示信息',
                btn: ['取消', '确认'],
                btn1:function(index){
                    layer.close(index)
                },
                btn2:function(){
                    wzERP.Common.deleteData('#delPositionUrl', $self)
                }
            }
        );
    })
};

//分页跳转
wzERP.Post.turnPage = function () {
    var opt = {
        pageUrl: $("#postQuery").val(),
        pageSearch: "#searchForm",
        //  pagesTotal: parseInt($('#pages').val()),
        //  pageCurr: parseInt($('#current').val()),
        //  pageSize: parseInt($('#pageSize').val()),
        //  currId:"#current"
    };
    wzERP.Common.pagingHandle(opt);
};

//表单验证器
wzERP.Post.verifier = function (_selector) {
    /*Arg Guide
     @_selector: 对象选择器
     */
    $(_selector).form({
        on: 'blur',
        // inline: true,
        transition: 'fade',
        fields: {
            //岗位名称
            positionName: {
                identifier: 'positionName',
                rules: [{
                    type: 'empty',
                    prompt: '请输岗位名称'
                }]
            },
            //直属上级
            positionParentId: {
                identifier: 'positionParentId',
                rules: [{
                    type: 'empty',
                    prompt: '请输直属上级'
                }]
            }
        }
    });
    
};